package android.slc.module.sys.ui.dict.data

import android.slc.appbase.ui.AppViewModel
import android.slc.appbase.ui.dialog.SlcFastDialogUtils
import android.slc.appbase.utils.AppIntentUtils
import android.slc.appdb.core.DbEntityMaster
import android.slc.appdb.sys.entity.CsDictData
import android.slc.appdb.sys.enums.StateEnum
import android.slc.appdb.sys.service.CsDictDataMapper
import android.slc.appdb.utils.DictEnumUtils
import android.slc.box.commonlibrary.compat.SlcResourceUtils
import javafx.beans.property.SimpleBooleanProperty
import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.SimpleObjectProperty
import javafx.beans.property.SimpleStringProperty
import org.apache.commons.lang3.StringUtils

open class DictDataEditVm : AppViewModel() {

    val isEditProperty = SimpleBooleanProperty()

    lateinit var csDictData: CsDictData

    val dictLabelProperty = SimpleStringProperty()

    val dictValueProperty = SimpleStringProperty()

    val dictTypeProperty = SimpleStringProperty()

    val dictStatusProperty = SimpleObjectProperty<Int>()

    val dictSortProperty = SimpleIntegerProperty()

    val dictRemarkProperty = SimpleStringProperty()

    open fun initVm() {
        val dataByBundle: Any? = AppIntentUtils.getDataByBundle(intentScope.bundle)
        if (dataByBundle is String) {
            this.csDictData = CsDictData()
            this.csDictData.dictType = dataByBundle
        } else if (dataByBundle is CsDictData) {
            this.csDictData = dataByBundle
        } else {
            SlcFastDialogUtils.createPrompt(SlcResourceUtils.getString("label_invalid_data")).build().showAndWait()
            finishView()
            return
        }
        this.dictLabelProperty.value = csDictData.dictLabel
        this.dictValueProperty.value = csDictData.dictValue
        this.dictTypeProperty.value = csDictData.dictType
        this.dictStatusProperty.value = csDictData._status
        this.dictSortProperty.value = csDictData.sort
        this.dictRemarkProperty.value = csDictData._remark
        this.isEditProperty.value = this.csDictData.eId != null
    }

    fun getStatusList(): List<String> {
        return DictEnumUtils.getLabelList(StateEnum.values())
    }

    fun setStatus(status: Int) {
        dictStatusProperty.value = status
    }

    open fun save() {
        if (StringUtils.isEmpty(this.dictLabelProperty.value)
            || StringUtils.isEmpty(this.dictValueProperty.value)
            || this.dictStatusProperty.value == null
        ) {
            SlcFastDialogUtils.showPrompt(SlcResourceUtils.getString("label_content_cannot_be_empty_blank"))
            return
        }
        this.csDictData.dictLabel = this.dictLabelProperty.value
        this.csDictData.dictValue = this.dictValueProperty.value
        this.csDictData.dictType = this.dictTypeProperty.value
        this.csDictData.sort = this.dictSortProperty.value
        this.csDictData._status = this.dictStatusProperty.value
        this.csDictData._remark = this.dictRemarkProperty.value
        try {
            DbEntityMaster.getService(CsDictDataMapper::class.java).put(this.csDictData)
            finishAndRefreshList()
        } catch (e: RuntimeException) {
            SlcFastDialogUtils.showPrompt(e.message ?: SlcResourceUtils.getString("label_save_failed"))
        }
    }

    /**
     * 开放
     */
    public override fun finish() {
        super.finish()
    }

    /**
     * 开放
     */
    public override fun finishView() {
        super.finishView()
    }
}